<template>
  <!-- 项目的最外层 -->
  <div class="outer">
    <!-- 窗口侧边栏 -->
    <Header />
    <!-- 头部 -->

    <!-- 商品分类导航 -->

    <!-- 购物车列表 -->
    <div class="cart">
      <h4>全部商品</h4>
      <div class="cart-main">
        <div class="cart-th">
          <div class="cart-th1">全部</div>
          <div class="cart-th2">商品</div>
          <div class="cart-th3">单价（元）</div>
          <div class="cart-th4">数量</div>
          <div class="cart-th5">小计（元）</div>
          <div class="cart-th6">操作</div>
        </div>
        <div class="cart-body">
          <ul
            class="cart-list"
            v-for="(cart, index) in cartInfoList"
            :key="cart.id"
          >
            <li class="cart-list-con1">
              <input
                @change="isChecked(cart, $event)"
                type="checkbox"
                name="chk_list"
                :checked="cart.isChecked == 1"
              />
            </li>
            <li class="cart-list-con2">
              <img :src="cart.imgUrl" />
              <div class="item-msg">
                {{ cart.skuName }}
              </div>
            </li>
            <li class="cart-list-con3">
              <div class="item-txt">语音升级款</div>
            </li>
            <li class="cart-list-con4">
              <span class="price">{{ cart.skuPrice }}.00</span>
            </li>
            <li class="cart-list-con5">
              <a
                href="javascript:void(0)"
                class="mins"
                @click="handel('sub', cart, -1)"
                >-</a
              >
              <input
                autocomplete="off"
                type="text"
                :value="cart.skuNum"
                minnum="1"
                class="itxt"
                @change="handel('changes', cart, $event.target.value)"
              />
              <a
                href="javascript:void(0)"
                class="plus"
                @click="handel('add', cart, 1)"
                >+</a
              >
            </li>
            <li class="cart-list-con6">
              <span class="sum">{{ cart.skuNum * cart.skuPrice }}</span>
            </li>
            <li class="cart-list-con7">
              <a class="sindelet" @click="removeList(cart)">删除</a>
              <br />
              <a href="#none">移到收藏</a>
            </li>
          </ul>
        </div>
      </div>
      <div class="cart-tool">
        <div class="select-all">
          <input
            @change="allChecked"
            class="chooseAll"
            type="checkbox"
            :Checked="ISCHECKED && cartInfoList.length > 0"
          />
          <span>全选</span>
        </div>
        <div class="option">
          <a @click="removeShopCart">删除选中的商品</a>
          <a href="#none">移到我的关注</a>
          <a href="#none">清除下柜商品</a>
        </div>
        <div class="money-box">
          <div class="chosed">已选择 <span>0</span>件商品</div>
          <div class="sumprice">
            <em>总价（不含运费） ：</em>
            <i class="summoney">{{ totalPrice }}</i>
          </div>
          <div class="sumbtn">
            <a class="sum-btn" @click="goTrade">结算</a>
          </div>
        </div>
      </div>
    </div>

    <!-- 底部 -->
  </div>
</template>

<script>
import Header from "@/components/Header.vue";
import throttle from "lodash/throttle";
import { mapGetters } from "vuex";
export default {
  name: "ShopCart",
  components: { Header },
  mounted() {
    this.getData();
  },
  methods: {
    goTrade() {
      this.$router.push("/Trade");
    },
    getData() {
      this.$store.dispatch("getShopCarList");
    },
    handel: throttle(async function (type, cart, disNum) {
      switch (type) {
        case "add":
          disNum = 1;
          break;
        case "sub":
          disNum = cart.skuNum > 1 ? -1 : 0;
          break;
        case "changes":
          if (isNaN(disNum) || disNum < 0) {
            disNum = 0;
          } else {
            disNum = parseInt(disNum) - cart.skuNum;
          }

          break;
      }
      try {
        await this.$store.dispatch("getAddorUpdataList", {
          skuId: cart.skuId,
          skuNum: disNum,
        });
        this.getData();
      } catch (error) {
        alert(error.message);
      }
    }, 500),

    async removeList(cart) {
      try {
        await this.$store.dispatch("removeCart", cart.skuId);
        this.getData();
      } catch (error) {
        alert(error.message);
      }
    },
    isChecked(cart, event) {
      try {
        let isChecked = event.target.checked ? 1 : 0;

        this.$store.dispatch("getChecked", {
          skuId: cart.skuId,
          isChecked: isChecked,
        });
        this.getData();
      } catch (error) {
        alert(error.message);
      }
    },
    async removeShopCart() {
      try {
        await this.$store.dispatch("removeShopCart");
        this.getData();
      } catch (error) {
        alert(error.message);
      }
    },
    async allChecked(e) {
      let isChecked = e.target.checked ? "1" : "0";
      try {
        await this.$store.dispatch("isAllCheckeds", isChecked);
        this.getData();
      } catch (error) {
        alert(error.message);
      }
    },
  },
  computed: {
    ...mapGetters(["getShopCarList"]),
    cartInfoList() {
      return this.getShopCarList.cartInfoList || [];
    },
    //计算总计多少钱
    totalPrice() {
      let sum = 0;
      this.cartInfoList.forEach((item) => {
        sum += item.skuNum * item.skuPrice;
      });
      return sum;
    },
    ISCHECKED() {
      return this.cartInfoList.every((item) => {
        return item.isChecked == 1;
      });
    },
  },
};
</script>

<style scoped>
.cart {
  width: 1200px;
  margin: 0 auto;
}
.cart h4 {
  margin: 9px 0;
  font-size: 14px;
  line-height: 21px;
}
.cart .cart-main .cart-th {
  background: #f5f5f5;
  border: 1px solid #ddd;
  padding: 10px;
  overflow: hidden;
}
.cart .cart-main .cart-th > div {
  float: left;
}
.cart .cart-main .cart-th .cart-th1 {
  width: 25%;
}
.cart .cart-main .cart-th .cart-th1 input {
  vertical-align: middle;
}
.cart .cart-main .cart-th .cart-th1 span {
  vertical-align: middle;
}
.cart .cart-main .cart-th .cart-th2 {
  width: 25%;
}
.cart .cart-main .cart-th .cart-th3,
.cart .cart-main .cart-th .cart-th4,
.cart .cart-main .cart-th .cart-th5,
.cart .cart-main .cart-th .cart-th6 {
  width: 12.5%;
}
.cart .cart-main .cart-body {
  margin: 15px 0;
  border: 1px solid #ddd;
}
.cart .cart-main .cart-body .cart-list {
  padding: 10px;
  border-bottom: 1px solid #ddd;
  overflow: hidden;
}
.cart .cart-main .cart-body .cart-list > li {
  float: left;
}
.cart .cart-main .cart-body .cart-list .cart-list-con1 {
  width: 4.1667%;
}
.cart .cart-main .cart-body .cart-list .cart-list-con2 {
  width: 25%;
}
.cart .cart-main .cart-body .cart-list .cart-list-con2 img {
  width: 82px;
  height: 82px;
  float: left;
}
.cart .cart-main .cart-body .cart-list .cart-list-con2 .item-msg {
  float: left;
  width: 150px;
  margin: 0 10px;
  line-height: 18px;
}
.cart .cart-main .cart-body .cart-list .cart-list-con3 {
  width: 20.8333%;
}
.cart .cart-main .cart-body .cart-list .cart-list-con3 .item-txt {
  text-align: center;
}
.cart .cart-main .cart-body .cart-list .cart-list-con4 {
  width: 12.5%;
}
.cart .cart-main .cart-body .cart-list .cart-list-con5 {
  width: 12.5%;
}
.cart .cart-main .cart-body .cart-list .cart-list-con5 .mins {
  border: 1px solid #ddd;
  border-right: 0;
  float: left;
  color: #666;
  width: 6px;
  text-align: center;
  padding: 8px;
}
.cart .cart-main .cart-body .cart-list .cart-list-con5 input {
  border: 1px solid #ddd;
  width: 40px;
  height: 33px;
  float: left;
  text-align: center;
  font-size: 14px;
}
.cart .cart-main .cart-body .cart-list .cart-list-con5 .plus {
  border: 1px solid #ddd;
  border-left: 0;
  float: left;
  color: #666;
  width: 6px;
  text-align: center;
  padding: 8px;
}
.cart .cart-main .cart-body .cart-list .cart-list-con6 {
  width: 12.5%;
}
.cart .cart-main .cart-body .cart-list .cart-list-con6 .sum {
  font-size: 16px;
}
.cart .cart-main .cart-body .cart-list .cart-list-con7 {
  width: 12.5%;
}
.cart .cart-main .cart-body .cart-list .cart-list-con7 a {
  color: #666;
}
.cart .cart-tool {
  overflow: hidden;
  border: 1px solid #ddd;
}
.cart .cart-tool .select-all {
  padding: 10px;
  overflow: hidden;
  float: left;
}
.cart .cart-tool .select-all span {
  vertical-align: middle;
}
.cart .cart-tool .select-all input {
  vertical-align: middle;
}
.cart .cart-tool .option {
  padding: 10px;
  overflow: hidden;
  float: left;
}
.cart .cart-tool .option a {
  float: left;
  padding: 0 10px;
  color: #666;
}
.cart .cart-tool .money-box {
  float: right;
}
.cart .cart-tool .money-box .chosed {
  line-height: 26px;
  float: left;
  padding: 0 10px;
}
.cart .cart-tool .money-box .sumprice {
  width: 200px;
  line-height: 22px;
  float: left;
  padding: 0 10px;
}
.cart .cart-tool .money-box .sumprice .summoney {
  color: #c81623;
  font-size: 16px;
}
.cart .cart-tool .money-box .sumbtn {
  float: right;
}
.cart .cart-tool .money-box .sumbtn a {
  display: block;
  position: relative;
  width: 96px;
  height: 52px;
  line-height: 52px;
  color: #fff;
  text-align: center;
  font-size: 18px;
  font-family: "Microsoft YaHei";
  background: #e1251b;
  overflow: hidden;
}
</style>
